package com.mage.controller;

import com.github.pagehelper.PageInfo;
import com.mage.model.User;
import com.mage.service.UserService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;
    
    @RequestMapping("add")
    public int insert(User user) {
        int i = userService.insert(user);
        return i;
    }

    @RequestMapping("delete")
    public int delete(Integer id) {
        int count = userService.delete(id);
        return count;
    }

    @RequestMapping("update")
    public int update(User user) {
        int count = userService.update(user);
        return count;
    }

    @RequestMapping("find/{id}")
    public User findById(Integer id) {
        User user = userService.findById(id);
        return user;

    }

    @RequestMapping("list")
    public List<User> find() {
        List<User> users = userService.find();
        return users;
    }

    @RequestMapping(path = {"p", "p/{page}/{size}", "p/{page}"})
    public PageInfo<User> selectForPage(@PathVariable(required = false) Integer page,
                                        @PathVariable(required = false)Integer size) {
        PageInfo<User> userPageInfo = userService.selectForPage(page, size);
        return userPageInfo;
    }
}
